package com.csdj.t309springboot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csdj.t309springboot.common.CacheConstant;
import com.csdj.t309springboot.entity.User;
import com.csdj.t309springboot.mapper.UserMapper;
import com.csdj.t309springboot.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.t309springboot.utils.EncryptUtil;
import com.csdj.t309springboot.utils.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 谭湛宇
 * @since 2021-12-31
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {

    private Logger log = LoggerFactory.getLogger("userservice");
    @Resource
    private RedisService redisService;
    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        String dePass = EncryptUtil.getMd5(password);
        if(StringUtils.hasText(dePass)){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(User::getUsername,username).eq(User::getPassword,dePass);
            User user = getOne(queryWrapper);
            if(user != null){
                setToken(user);
                redisService.setString(CacheConstant.TOKEN_PREFIX+user.getId(),user.getToken(),1800);
                return user;
            }
        }
        return null;
    }

    private void setToken(User user){
        if(user != null){
            user.setToken(RandomUtil.genToken());
        }
    }
    /**
     *
     * @param id
     * @return
     */
    @Cacheable(key = "#id",unless = "#result == null")
    public User getUserById(int id){
        log.info("执行查询");
        User user = userMapper.selectById(id);
        return user;
    }
    @CacheEvict( key = "#user.id")
    public boolean update(User user){
        int res = userMapper.updateById(user);
        if(res>0){
            //修改成功后
            return true;
        }
        return false;
    }

    public int insert(User user){
        return userMapper.insert(user);
    }

    public int delete(int id){
        return userMapper.deleteById(id);
    }

    public List<User> getAllUsers(){
//        return userMapper.selectList(null);
        return userMapper.selectList(null);
    }

    public User getUserInfoById(int id){
        User user = userMapper.selectById(id);
        user.setPassword(null);
        return user;
    }

    /**
     * 模糊查询
     */
    public List<User> getAllUsersByMoHuChaXun(String column){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("username",column);

        return userMapper.selectList(queryWrapper);
    }




}
